package DAO;

import java.util.ArrayList;

import DTO.AlbumModel;
import DTO.PhotoModel;
import Help.Manager.Album.PhotoHelpers;





/*
 * Class AlbumsController há»— trá»£ viá»‡c táº¡o danh sÃ¡ch Album.
 */
public class AlbumsController {
	/*
	 * Danh sÃ¡ch Album.
	 */
	private ArrayList<AlbumModel> albums;
	
	public AlbumsController() {
		this.albums = new ArrayList<AlbumModel>();
	}
	
	/*
	 * Táº¡o danh sÃ¡ch Album dá»±a vÃ o danh sÃ¡ch Photo.
	 * Album Ä‘Æ°á»£c táº¡o dá»±a vÃ o thÆ° má»¥c cha cá»§a Photo.
	 */
	public ArrayList<AlbumModel> createAlbums(ArrayList<PhotoModel> photos) {
		this.albums.clear();
		
		for(PhotoModel photo : photos) {
			String path = photo.getPath();
			
			String albumPath = PhotoHelpers.getParentPath(path);
			String albumName = PhotoHelpers.getParentFolderName(path);
			
			AlbumModel album = this.getAlbum(albumPath);
			
			// Táº¡o má»›i album náº¿u chÆ°a cÃ³.
			if(album == null) {
				album = new AlbumModel();
				album.setPath(albumPath);
				album.setTitle(albumName);
				this.albums.add(album);
			}
			
			album.getPhotos().add(photo);
		}
		
		return this.albums;
	}

	
	/*
	 * TÃ¬m má»™t Album dá»±a vÃ o path.
	 * Tráº£ vá»� null náº¿u khÃ´ng tÃ¬m thÃ¡y.
	 */
	public AlbumModel getAlbum(String path) {
		for(AlbumModel album : this.albums) {
			if(album.getPath().equals(path))
				return album;
		}
		return null;
	}
	
	public void removePhoto(PhotoModel photo, AlbumModel album) {
		album.getPhotos().remove(photo);
		if(album.getPhotos().size() == 0)
			this.albums.remove(album);
	}
	
	public void removeAlbums(AlbumModel album) {
		this.albums.remove(album);
	}
	
	public ArrayList<AlbumModel> getAlbums() {
		return this.albums;
	}
	
	public AlbumModel getAlbum(int position) {
		return this.albums.get(position);
	}
}
